package com.itcam.interview.pro00.nullpoint;

import java.util.Optional;

/**
 * @author : Cammy.Wu
 * Description : 使用Opetional解决层次多的问题
 */

public class _01_避免空指针 {
    public static void main(String[] args) {
        Address address = new Address();
        User user = new User("Cammy", 20, address);

//        extracted(user);
        Optional.ofNullable(user)
                .map(User::getAddress)
                .map(Address::getCity)
                .map(String::trim)
                .orElse(getDefaultCity());
    }

    private static String getDefaultCity() {
        System.out.println("初始化城市信息");
        return "beijing";
    }

    private static void extracted(User user) {
        if (null != user) {
            Address address1 = user.getAddress();
            if (null != address1) {
                String city = address1.getCity();
                if (null != city && city.equals("")) {
                    System.out.println("city = " + city);
                }
            }
        }
    }
}
